package com.analysis.service;

import com.analysis.entity.ProjectBrand;
import com.analysis.mapper.ProjectBrandMapper;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service
public class ProjectBrandService {

    @Autowired
    private ProjectBrandMapper projectBrandMapper;

    public int create(ProjectBrand projectBrand) {
        if (StringUtils.isBlank(projectBrand.getProductName())) {
            throw new IllegalArgumentException("品牌名称不存在！");
        }
        // 检查filter_name是否重复
//        if (StringUtils.isNotBlank(projectBrand.getFilterName())) {
//            ProjectBrand existingBrand = getByProjectIdAndFilterName(projectBrand.getProjectId(), projectBrand.getFilterName());
//            if (existingBrand != null) {
//                throw new IllegalArgumentException("品牌名称已存在，请使用其他名称");
//            }
//        }
        projectBrand.setIsSelected(1);
        return projectBrandMapper.insert(projectBrand);
    }

    public int delete(Long id) {
        return projectBrandMapper.deleteById(id);
    }

    public int update(ProjectBrand projectBrand) {
        return projectBrandMapper.update(projectBrand);
    }

    public ProjectBrand getById(Long id) {
        return projectBrandMapper.selectById(id);
    }

    public ProjectBrand selectByProjectId(Long projectId) {
        List<ProjectBrand> projectBrands = projectBrandMapper.selectByProjectId(projectId);
        if (projectBrands != null) {
            return projectBrands.get(0);
        }
        return null;
    }

    public List<ProjectBrand> listByProjectIds(List<Long> projectIds) {
        if (projectIds == null || projectIds.isEmpty()) {
            return new ArrayList<>();
        }
        return projectBrandMapper.selectByProjectIds(projectIds);
    }

    public List<ProjectBrand> page(Long projectId, int pageNum, int pageSize) {
        int offset = (pageNum - 1) * pageSize;
        return projectBrandMapper.selectByPage(projectId, offset, pageSize);
    }

    public int count(Long projectId) {
        return projectBrandMapper.countByProjectId(projectId);
    }

    public ProjectBrand getByProjectIdAndFilterName(Long projectId, String filterName) {
        return projectBrandMapper.selectByProjectIdAndFilterName(projectId, filterName);
    }

    /**
     * 根据项目ID查询当前选中的品牌
     */
    public ProjectBrand getSelectedByProjectId(Long projectId) {
        return projectBrandMapper.selectSelectedByProjectId(projectId);
    }

    /**
     * 设置品牌为选中状态（会先取消其他品牌的选中状态）
     */
    public int selectBrand(Long projectId, Long brandId) {
        // 先取消项目所有品牌的选中状态
        projectBrandMapper.unselectAllByProjectId(projectId);
        // 再设置指定品牌为选中状态
        return projectBrandMapper.selectBrand(brandId);
    }

    /**
     * 取消项目所有品牌的选中状态
     */
    public int unselectAllByProjectId(Long projectId) {
        return projectBrandMapper.unselectAllByProjectId(projectId);
    }
}
